package com.ruoyi.mall.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.domain.bo.ShopBo;
import com.ruoyi.mall.domain.vo.ShopVo;
import com.ruoyi.mall.domain.Shop;
import com.ruoyi.mall.mapper.ShopMapper;
import com.ruoyi.mall.service.IShopService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 店铺信息Service业务层处理
 *
 * @author xie
 * @date 2022-09-06
 */
@RequiredArgsConstructor
@Service
public class ShopServiceImpl implements IShopService {

    private final ShopMapper baseMapper;

    /**
     * 查询店铺信息
     */
    @Override
    public ShopVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询店铺信息列表
     */
    @Override
    public TableDataInfo<ShopVo> queryPageList(ShopBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Shop> lqw = buildQueryWrapper(bo);
        Page<ShopVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询店铺信息列表
     */
    @Override
    public List<ShopVo> queryList(ShopBo bo) {
        LambdaQueryWrapper<Shop> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Shop> buildQueryWrapper(ShopBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Shop> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, Shop::getDeptId, bo.getDeptId());
        lqw.eq(bo.getUserId() != null, Shop::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getAppid()), Shop::getAppid, bo.getAppid());
        lqw.eq(bo.getUid() != null, Shop::getUid, bo.getUid());
        lqw.eq(StringUtils.isNotBlank(bo.getEnterType()), Shop::getEnterType, bo.getEnterType());
        lqw.like(StringUtils.isNotBlank(bo.getShopName()), Shop::getShopName, bo.getShopName());
        lqw.eq(StringUtils.isNotBlank(bo.getShopLogo()), Shop::getShopLogo, bo.getShopLogo());
        lqw.eq(StringUtils.isNotBlank(bo.getShopBg()), Shop::getShopBg, bo.getShopBg());
        lqw.eq(StringUtils.isNotBlank(bo.getImgs()), Shop::getImgs, bo.getImgs());
        lqw.eq(StringUtils.isNotBlank(bo.getLabel()), Shop::getLabel, bo.getLabel());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), Shop::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getTel()), Shop::getTel, bo.getTel());
        lqw.eq(bo.getLongitude() != null, Shop::getLongitude, bo.getLongitude());
        lqw.eq(bo.getLatitude() != null, Shop::getLatitude, bo.getLatitude());
        lqw.eq(bo.getSendFee() != null, Shop::getSendFee, bo.getSendFee());
        lqw.eq(StringUtils.isNotBlank(bo.getSendTime()), Shop::getSendTime, bo.getSendTime());
        lqw.eq(StringUtils.isNotBlank(bo.getIntro()), Shop::getIntro, bo.getIntro());
        lqw.eq(StringUtils.isNotBlank(bo.getNotice()), Shop::getNotice, bo.getNotice());
        lqw.eq(StringUtils.isNotBlank(bo.getKfs()), Shop::getKfs, bo.getKfs());
        lqw.eq(bo.getScore() != null, Shop::getScore, bo.getScore());
        lqw.eq(bo.getCommentNum() != null, Shop::getCommentNum, bo.getCommentNum());
        lqw.eq(bo.getMonthSale() != null, Shop::getMonthSale, bo.getMonthSale());
        lqw.eq(bo.getTotalSale() != null, Shop::getTotalSale, bo.getTotalSale());
        lqw.eq(bo.getWeight() != null, Shop::getWeight, bo.getWeight());
        lqw.eq(bo.getRatio() != null, Shop::getRatio, bo.getRatio());
        lqw.eq(StringUtils.isNotBlank(bo.getIsPutaway()), Shop::getIsPutaway, bo.getIsPutaway());
        lqw.eq(StringUtils.isNotBlank(bo.getPutawayReason()), Shop::getPutawayReason, bo.getPutawayReason());
        lqw.eq(bo.getPutawayUid() != null, Shop::getPutawayUid, bo.getPutawayUid());
        lqw.eq(bo.getPutawayTime() != null, Shop::getPutawayTime, bo.getPutawayTime());
        String searchValue=bo.getSearchValue();
        if(StringUtils.isNotEmpty(searchValue)){
            lqw.and(w->w
                        .or()
                        .like(Shop::getDeptId,searchValue)

                        .or()
                        .like(Shop::getUserId,searchValue)

                        .or()
                        .like(Shop::getAppid,searchValue)

                        .or()
                        .like(Shop::getUid,searchValue)

                        .or()
                        .like(Shop::getEnterType,searchValue)

                        .or()
                        .like(Shop::getShopName,searchValue)

                        .or()
                        .like(Shop::getShopLogo,searchValue)

                        .or()
                        .like(Shop::getShopBg,searchValue)

                        .or()
                        .like(Shop::getImgs,searchValue)

                        .or()
                        .like(Shop::getLabel,searchValue)

                        .or()
                        .like(Shop::getAddress,searchValue)

                        .or()
                        .like(Shop::getTel,searchValue)

                        .or()
                        .like(Shop::getLongitude,searchValue)

                        .or()
                        .like(Shop::getLatitude,searchValue)

                        .or()
                        .like(Shop::getSendFee,searchValue)

                        .or()
                        .like(Shop::getSendTime,searchValue)

                        .or()
                        .like(Shop::getIntro,searchValue)

                        .or()
                        .like(Shop::getNotice,searchValue)

                        .or()
                        .like(Shop::getKfs,searchValue)

                        .or()
                        .like(Shop::getScore,searchValue)

                        .or()
                        .like(Shop::getCommentNum,searchValue)

                        .or()
                        .like(Shop::getMonthSale,searchValue)

                        .or()
                        .like(Shop::getTotalSale,searchValue)

                        .or()
                        .like(Shop::getWeight,searchValue)

                        .or()
                        .like(Shop::getRatio,searchValue)

                        .or()
                        .like(Shop::getIsPutaway,searchValue)

                        .or()
                        .like(Shop::getPutawayReason,searchValue)

                        .or()
                        .like(Shop::getPutawayUid,searchValue)

//                        .or()
//                        .like(Shop::getPutawayTime,searchValue)

            );
        }
        return lqw;
    }

    /**
     * 新增店铺信息
     */
    @Override
    public Boolean insertByBo(ShopBo bo) {
        Shop add = BeanUtil.toBean(bo, Shop.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改店铺信息
     */
    @Override
    public Boolean updateByBo(ShopBo bo) {
        Shop update = BeanUtil.toBean(bo, Shop.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Shop entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除店铺信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
